<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/24
 * Time: 15:07
 */
namespace common\components\services\restclient;

use common\components\exception\DomainIsNullException;
use common\components\helpers\Fun;
use common\components\helpers\Util;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;

class FrontRest extends BaseRest
{
    private $domain;

    /* private function getSite()
     {
         $site_id = Fun::getSiteId();//获取当前分店ID
         $this->domain = BaseRest::site();//可
     }

     public static function testUrl(){
         \Yii::$app->set('urlManager',[
             'enablePrettyUrl' => true,
             'showScriptName' => false,
             'enableStrictParsing' => true,
             'rules' => [
                 'http://<site:\w+>.doumiaoduo.cn/<module:\w+>-<controller:\w+>-<action:\w+>' => '<module>/<controller>/<action>',
             ],
         ]);
         echo Url::to(['/api/test/test','site'=>'aks']);
     }

     public static function getA(){
         $url='';
         return BaseRest::httpGet($url);
     }*/

    /*
     * 合成API的url
     * $square 广场
     * $c 控制器
     * $a 方法
     * */
    /*public static function url($square, $c, $a = '')
    {
        empty($a)aoduo.cn/api-' . $c . '-index' : $url = 'http://' . $square . '.doumiaoduo.cn/api-' . $c . '-' . $a;
        return $url; ? $url = 'http://' . $square . '.doumi
    }*/

    public static function url($c, $a = '')
    {
        $square_domain = Fun::getDomain();
        if (empty($square_domain)) throw new DomainIsNullException('域名不存在');
        $url_info = \Yii::$app->params['http_square_api_rest_api'];
        $url = empty($a) ? $url_info['http'] . $square_domain . $url_info['url'] . $c . '-index' : $url_info['http'] . $square_domain . $url_info['url'] . $c . '-' . $a;
        return $url;
    }

    public static function listPta()
    {
        $url = FrontRest::url('pta', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listPtaActive()
    {
        $url = FrontRest::url('ptaactive', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listMechanic()
    {
        $url = FrontRest::url('mechanic', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listOrder()
    {
        $url = FrontRest::url('order', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listTeacher()
    {
        $url = FrontRest::url('teacher', "index");
        return BaseRest::httpGet($url);
    }

    public static function getTeacher($id)
    {
        $url = FrontRest::url('teacher', "view") . "?id=" . $id;
        return BaseRest::httpGet($url);
    }

    public static function getCourse($id)
    {
        $url = FrontRest::url('course', "index") . "?teach_form=" . $id;
        return BaseRest::httpGet($url);
    }

    public static function listDepartment()
    {
        $url = FrontRest::url('department', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listCommentinfo()
    {
        $url = FrontRest::url('commentinfo', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listAdmin()
    {
        $url = FrontRest::url('admin', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listManager()
    {
        $url = FrontRest::url('admin', 'findbymanager');
        return BaseRest::httpGet($url);
    }

    public static function listClassTeacher()
    {
        $url = FrontRest::url('admin', 'findbyteacher');
        return BaseRest::httpGet($url);
    }


    public static function listClassroom()
    {
        $url = FrontRest::url('classroom', 'index');
        return BaseRest::httpGet($url);
    }

    public static function findByClassroomId($classroom_id)
    {
        $url = FrontRest::url('classroom', 'findbyclassroomid') . "?classroom_id=" . $classroom_id;;
        return BaseRest::httpGet($url);
    }

    public static function listClassroomAttr()
    {
        $url = FrontRest::url('classroomattr', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listCourse()
    {
        $url = FrontRest::url('course', 'index');
        return BaseRest::httpGet($url);
    }

    public static function listOrderCourserHistory()
    {
        $url = FrontRest::url('ordercoursehistory', 'index');
        return BaseRest::httpGet($url);
    }

    public static function findByCode($v_code)
    {
        $url = FrontRest::url('ordercoursehistory', 'findbycode') . "?v_code=" . $v_code;
        return BaseRest::httpGet($url);
    }

    public static function listStudent()
    {
        $url = FrontRest::url('student', 'index');
        return BaseRest::httpGet($url);
    }

    public static function findByStudentId($customer_id)
    {
        $url = FrontRest::url('student', 'findbycustomerid') . "?customer_id=" . $customer_id;
        return BaseRest::httpGet($url);
    }
	
	 public static function viewStudent($id)
    {
        $url = FrontRest::url('student', 'view') . "?id=" . $id;
        return BaseRest::httpGet($url);
    }

    public static function listOrderHistory()
    {
        $url = FrontRest::url('orderhistory', 'index');
        return BaseRest::httpGet($url);
    }

    public static function getOrderHistory($order_id)
    {
        $url = FrontRest::url('orderhistory', 'findbyorderid') . "?order_id=" . $order_id;
        return BaseRest::httpGet($url);
    }

    public static function createPtaProposal($data)
    {
        $url = FrontRest::url('ptaproposal', 'create');
        return BaseRest::httpPost($url, $data);
    }


    public static function createPtaActive($data)
    {
        $url = FrontRest::url('ptaactive', 'create');
        return BaseRest::httpPost($url, $data);
    }

    public static function createStudent($data)
    {
        $url = FrontRest::url('student', 'create');
        return BaseRest::httpPost($url, $data);
    }

    public static function createOrder($data)
    {
        $url = FrontRest::url('order', 'create');
        return BaseRest::httpPost($url, $data);
    }

    public static function updateOrder($data)
    {
        $url = FrontRest::url('order', 'update');
        return BaseRest::httpPut($url, $data);
    }

    public static function createOrderSales($data)
    {
        $url = FrontRest::url('ordersales', 'create');
        return BaseRest::httpPost($url, $data);
    }

    public static function updateOrderSales($data)
    {
        $url = FrontRest::url('ordersales', 'update');
        return BaseRest::httpPut($url, $data);
    }

    public static function createComment($data)
    {
        $url = FrontRest::url('comment', 'create');
        return BaseRest::httpPost($url, $data);
    }

    public static function createOrderHistory($data)
    {
        $url = FrontRest::url('orderhistory', 'create');
        return BaseRest::httpPost($url, $data);
    }
	
	public static function sendMsg($data)
	{
        $url = FrontRest::url('order','send');
        return BaseRest::httpPost($url, $data);
    }

    /* public static $squareUrl='.yijiasan.com/api';

     public static $backstageUrl='http://localhost/backend/index.php?r=api/';

     public static function SquareRest($city_id,$modelClass){
         $square=BackRest::BackstageRest('site');
         $website_data=Fun::cache('website_cache');
         $data_arr=[];
         foreach($website_data as $wkey=>$wval){
             foreach($square as $skey=>$sval){
                 if($city_id==$wval['id']){
                     if($city_id==$sval['web_site_id']){
                         $data_arr[$wval['name']][$sval['domain']]=$sval;
                         $data_arr[$wval['name']][$sval['domain']]['square_url']=$sval['domain'].self::$squareUrl.'-'.$modelClass.'-index';
                         $data_arr[$wval['name']][$sval['domain']]['data']=BaseRest::httpGet($sval['domain'].self::$squareUrl.'-'.$modelClass.'-index');
                     }
                 }
             }
         }
         return $data_arr;
     }

     public static function Tags(){
         $square=self::FrontstageRest('tags');
         return $square;
     }

     public static function FrontstageRest($modelClass){
         $url=self::$backstageUrl.$modelClass.'/index';
         if(BaseRest::httpGet($url)==null){
             throw new NotFoundHttpException(\Yii::t('backend','数据不存在'));
         }
         return BaseRest::httpGet($url);
     }*/

}